﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.Views;
using EHR.Common;

namespace CommunitySystem
{
    public partial class ShenPiControl2 : AsyncCtrl, IReturnListenerc
    {
        #region IReturnListenerc 成员
        public void BeginReturn()
        {
            Loaded();
        }
        #endregion
        public NavMoveSwitch Nav { get; set; }
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        public ObservableCollection<RenQianTaoLun> models = new ObservableCollection<RenQianTaoLun>();
        public int OrgID = 0;
        private string OrgName = "";
        public ShenPiControl2(int orgID, string orgName)
        {
            InitializeComponent();
            OrgID = orgID;
            OrgName = orgName;
            if (Session.LoginUser.LoginName == "admin")
            {
                OKButton.Visibility = System.Windows.Visibility.Collapsed;
                scrol.IsEnabled = false;
            }
            else
            {
                OKButton.Visibility = System.Windows.Visibility.Visible;
                next.IsEnabled = false;
                scrol.IsEnabled = true;
            }
            Loaded();
        }
        public void Loaded()
        {
            Client.GetRenQianTaoLunCompleted -= new EventHandler<GetRenQianTaoLunCompletedEventArgs>(Client_GetRenQianTaoLunCompleted);
            Client.GetRenQianTaoLunCompleted += new EventHandler<GetRenQianTaoLunCompletedEventArgs>(Client_GetRenQianTaoLunCompleted);
            Client.GetRenQianTaoLunAsync(OrgID);
        }

        void Client_GetRenQianTaoLunCompleted(object sender, GetRenQianTaoLunCompletedEventArgs e)
        {
            stackpanel.Children.Clear();
            if (e.Result != null && e.Result.Count > 0)
            {
                OKButton.IsEnabled = true;
                if (e.Result[0].ID != 0) next.IsEnabled = true;
                models = e.Result;

                TextBlock tb1 = new TextBlock();
                tb1.Text = "            经党（工）委（组）";
                tb1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                tb1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                
                TextBox t = new TextBox();
                if (!string.IsNullOrEmpty(e.Result[0].SNian)) t.Text = e.Result[0].SNian;
                t.TextChanged += new TextChangedEventHandler(t_TextChanged);
                t.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                t.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                t.Width = 60;
                t.Height = 25;
                Binding b1 = new Binding();
                b1.Source = e.Result[0];
                b1.Path = new PropertyPath("SNian");
                b1.Mode = BindingMode.TwoWay;
                BindingOperations.SetBinding(t, TextBox.TextProperty, b1);
                
                TextBlock tb2 = new TextBlock();
                tb2.Text = "年";
                tb2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                tb2.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                TextBox t1 = new TextBox();
                if (!string.IsNullOrEmpty(e.Result[0].SYUe)) t1.Text = e.Result[0].SYUe;
                t1.TextChanged += new TextChangedEventHandler(t_TextChanged);
                t1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                t1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                t1.Width = 60;
                t1.Height = 25;
                Binding b2 = new Binding();
                b2.Source = e.Result[0];
                b2.Path = new PropertyPath("SYUe");
                b2.Mode = BindingMode.TwoWay;
                BindingOperations.SetBinding(t1, TextBox.TextProperty, b2);
                
                TextBlock tb3 = new TextBlock();
                tb3.Text = "月";
                tb3.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                tb3.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                TextBox t2 = new TextBox();
                if (!string.IsNullOrEmpty(e.Result[0].SRi)) t2.Text = e.Result[0].SRi;
                t2.TextChanged += new TextChangedEventHandler(t_TextChanged);
                t2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                t2.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                t2.Width = 60;
                t2.Height = 25;
                Binding b3 = new Binding();
                b3.Source = e.Result[0];
                b3.Path = new PropertyPath("SRi");
                b3.Mode = BindingMode.TwoWay;
                BindingOperations.SetBinding(t2, TextBox.TextProperty, b3);
                

                TextBlock tb4 = new TextBlock();
                tb4.Text = "日会议讨论决定如下： ";
                tb4.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                tb4.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                StackPanel s = new StackPanel();
                s.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                
                s.Orientation = Orientation.Horizontal;
                s.Children.Add(tb1); s.Children.Add(t); s.Children.Add(tb2); s.Children.Add(t1); s.Children.Add(tb3); s.Children.Add(t2); s.Children.Add(tb4);

                stackpanel.Children.Add(s);

                int count = ((ObservableCollection<RenQianTaoLun>)e.Result).Count;
                for (int i = 0; i < count; i++)
                {
                    TextBlock tb = new TextBlock();
                    tb.Margin = new Thickness(120, 3, 3, 3);
                    tb.Text = e.Result[i].EmpName;
                    tb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                    ComboBox cb = new ComboBox();
                    cb.Tag = e.Result[i];
                    cb.SelectionChanged += new SelectionChangedEventHandler(cb_SelectionChanged);
                    cb.Width = 90;
                    cb.Height = 25;
                    ComboBoxItem ci = new ComboBoxItem();
                    ci.Content = "同意";
                    ComboBoxItem ci1 = new ComboBoxItem();
                    ci1.Content = "不同意";
                    cb.Items.Add(ci);
                    cb.Items.Add(ci1);

                    if (!string.IsNullOrEmpty(e.Result[i].IsTongYi))
                    {
                        if (e.Result[i].IsTongYi == "同意")
                        {
                            cb.SelectedIndex = 0;
                        }
                        else
                        {
                            cb.SelectedIndex = 1;
                        }
                    }
                    cb.Margin = new Thickness(3, 3, 3, 3);
                    //Binding b = new Binding();
                    //b.Source = e.Result[i];
                    //b.Path = new PropertyPath("IsTongYi");
                    //b.Mode = BindingMode.TwoWay;
                    //BindingOperations.SetBinding(cb, ComboBox.SelectedItemProper, b);
                    

                    TextBlock tb1s = new TextBlock();
                    tb1s.Margin = new Thickness(3, 3, 3, 3);
                    tb1s.Text = "转任";
                    tb1s.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb1s.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                    TextBlock tbs = new TextBlock();
                    if (!string.IsNullOrEmpty(e.Result[i].Duty)) tbs.Text = e.Result[i].Duty;
                    tbs.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tbs.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    tbs.Margin = new Thickness(3, 3, 3, 3);


                    StackPanel sp = new StackPanel();
                    sp.Orientation = Orientation.Horizontal;
                    sp.Margin = new Thickness(3, 3, 3, 3);
                    sp.Children.Add(tb);
                    sp.Children.Add(cb);
                    sp.Children.Add(tb1s);
                    sp.Children.Add(tbs);

                    stackpanel.Children.Add(sp);
                }

                TextBlock tb5 = new TextBlock();
                tb5.Text = "    特此证明";
                tb5.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                tb5.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                stackpanel.Children.Add(tb5);
            }
            else OKButton.IsEnabled = false;
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        void t_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern);   // 匹配正则表达式
            if (!m.Success)   // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox)sender).Text = "";
                return;
            }
        }

        void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((RenQianTaoLun) ((ComboBox) sender).Tag).IsTongYi = ((System.Windows.Controls.ContentControl)
                                                                  (((ComboBoxItem) ((ComboBox) sender).SelectedItem))).
                Content.
                ToString();
        }
        private int c = 0;
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定上报会议结果吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                foreach (RenQianTaoLun i in models)
                {
                    Client.InsertRenQianTaoLunCompleted -= new EventHandler<InsertRenQianTaoLunCompletedEventArgs>(Client_InsertRenQianTaoLunCompleted);
                    Client.InsertRenQianTaoLunCompleted += new EventHandler<InsertRenQianTaoLunCompletedEventArgs>(Client_InsertRenQianTaoLunCompleted);
                    Client.InsertRenQianTaoLunAsync(i);
                    c += 1;
                }
            }
        }

        void Client_InsertRenQianTaoLunCompleted(object sender, InsertRenQianTaoLunCompletedEventArgs e)
        {
            if (c == models.Count)
            {
                c = c - 1;
                ShenPiControl3 view = new ShenPiControl3(Convert.ToInt32(OrgID), OrgName);
                view.Nav = Nav;
                Nav.ShowNextPage(OrgName + "-公示情况", view, this);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            ShenPiControl3 view = new ShenPiControl3(Convert.ToInt32(OrgID), OrgName);
            view.Nav = Nav;
            Nav.ShowNextPage(OrgName + "-公示情况", view, this);
        }
    }
}
